#include <cstdio>
#include <cstring>

int total, n, w[ 31 ], f[ 3000001 ];

void init(  )
{
    int i, j, k, sum, p1, p2, q1, q2, a, b, c;
    double t;
    bool can;
    char data[ 20 ];
    for ( i = 0; i < n; i++ )
    {
        scanf("%d", &k);
        sum = a = b = c = 0;
        can = true;
        for ( j = 0; j < k; j++ )
        {
            scanf("%s", data);
            if ( data[ 0 ] < 'A' || data[ 0 ] > 'C' )
            {
                can = false;
                continue;
            }
            if ( ( data[ 2 ] < '0' || data[ 2 ] > '9' ) && data[ 2 ] != '.' )
            {
                can = false;
                continue;
            }
            p1 = p2 = 0;
            for ( q1 = 2; q1 < strlen( data ); q1++ )
            {
                if ( data[ q1 ] == '.' )
                    break;
                p1 = p1 * 10 + data[ q1 ] - '0';
            }
            for ( q2 = q1 + 1; q2 < strlen( data ) && q2 < q1 + 3; q2++ )
            {
                if ( q2 == q1 + 1 )
                    p2 = ( data[ q2 ] - '0' ) * 10;
                else
                    p2 += data[ q2 ] - '0';
            }
            p1 *= 100;
            sum = sum + p1 + p2;
            if ( data[ 0 ] == 'A' )
                a += p1 + p2;
            if ( data[ 0 ] == 'B' )
                b += p1 + p2;
            if ( data[ 0 ] == 'C' )
                c += p1 + p2;
        }
        if ( a > 60000 || b > 60000 || c > 60000 )
            can = false;
        if ( sum > 100000 )
            can = false;
        if ( can )
            w[ i ] = sum;
        else
            w[ i ] = total + 1;
    }
    for ( i = 0; i <= total; i++ )
        f[ i ] = 0;
    f[ 0 ] = 1;
}

void work( )
{
    int i, j;
    for ( i = 0; i < n; i++ )
        for ( j = total; j >= w[ i ]; j-- )
            f[ j ] = ( f[ j ] || f[ j - w[ i ] ] );
}

void print( )
{
    int i;
    for ( i = total; i >= 0; i-- )
        if ( f[ i ] )
            break;
    printf("%.2lf\n", double( i ) / 100);
}

int main( )
{
    double t;
    while ( scanf("%lf%d", &t, &n) != EOF )
    {
        if ( n == 0 )
            break;
        total = int( t * 100 );
        init( );
        work( );
        print( );
    }
    return 0;
}
